package com.pimee.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.pimee.support.shiro.security.UserToken;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping(value = "/admin")
public class LoginController {

	@ApiOperation("登录操作")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object submitLogin(ModelMap mmap, String username, String password, HttpServletRequest request) {
        try {
        	UserToken token = new UserToken(username, password, "Admin");
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
//            SysUser user = (SysUser) subject.getPrincipal();
        } catch (DisabledAccountException e) {
        	log.error(e.getMessage(), e);
            request.setAttribute("msg", "账户已被禁用");
            return "admin/login";
        } catch (AuthenticationException e) {
        	log.error(e.getMessage(), e);
            request.setAttribute("msg", "用户名或密码错误");
            return "admin/login";
        }
        Map<String, Object> result = Maps.newHashMap();
        result.put("code", 200);
        // 执行到这里说明用户已登录成功
        return result;
    }

    //被踢出后跳转的页面
    @RequestMapping(value = "/kickout", method = RequestMethod.GET)
    public String kickOut() {
        return "kickout";
    }
}
